package com.sptpc.learnspring.service.impl;

import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.repository.DrinkRepository;
import com.sptpc.learnspring.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DrinkServiceImpl implements IDrinkService {
    @Autowired
    private DrinkRepository drinkRepository;

    @Override
    public List<Drink> getAllDrinks() {
        return drinkRepository.findAll(Sort.by(Sort.Direction.DESC, "createTime"));
    }

    @Override
    public Drink addNewDrink(Drink drink) {
        return drinkRepository.saveAndFlush(drink);
    }

    @Override
    public Optional<Drink> getDrinkById(Long id) {
        return drinkRepository.findById(id);
    }

    @Override
    public List<Drink> searchDrinksByKey(String key) {
        return drinkRepository.findDrinksByNameLike(key);
    }

    @Override
    public List<Drink> searchDrinksByCategory(Long cid) {
        return drinkRepository.findDrinksByCategory_Id(cid);
    }

    @Override
    public List<Drink> getDrinkByIds(List<Long> ids) {
        return drinkRepository.findDistinctByIdIn(ids);
    }
}
